<?php
/**
 * Created by PhpStorm.
 * Date: 2020/11/6
 * Time: 14:57
 */

namespace utils;


use Firebase\JWT\JWT;
use think\facade\Config;

class JwtAuth
{
    /**
     * @param $adminInfo
     * @param $type
     * @param array $params
     * @return array
     */
    public static function setToken($adminInfo, $type, $params = []): array
    {
        $id = $adminInfo['id'];
        $host = app()->request->host();
        $time = time();
        $params += [
            'iss' => $host,
            'aud' => $host,
            'iat' => $time,
            'nbf' => $time,
            'exp' => strtotime('+ 12hour'),
        ];
        $params['jti'] = compact('id', 'type');
        $token = JWT::encode($params, Config::get('app.app_key', 'default'));

        return compact('token', 'params');
    }

    public static function parseToken(string $jwt): array
    {
        JWT::$leeway = 60;

        $data = JWT::decode($jwt, Config::get('app.app_key', 'default'), array('HS256'));
//        return [AdminUser::where('id', $data->jti->id)->find(), $data->jti->type];
    }
}
